package com.shoppingmall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shoppingmall.execption.PasswordException;
import com.shoppingmall.execption.UsernameNotFoundException;
import com.shoppingmall.mapper.AdminMapper;
import com.shoppingmall.model.Admin;
import com.shoppingmall.model.LoginParam;
import com.shoppingmall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
        private   AdminMapper adminMapper;
    @Override
    public Admin login(LoginParam loginParam) {
        //根据用户名获得用户信息
        Admin admin = adminMapper.getAdminByName(loginParam.getUsername());
        //判断用户名是否正确
        if(admin == null){
            //使用异常处理用户名不存在
            throw new UsernameNotFoundException();
        }

        //判断密码是否正确
        if(!admin.getAdmin_password().equals(loginParam.getPassword())){
            //使用异常处理密码错误
            throw new PasswordException();
        }
        admin.setAdmin_password(null);
        return admin;
    }
}
